package com.example.components.config;

import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

/**
 * @author raven
 * @since 2022/1/11
 */
@Configuration
@MapperScan("com.example.components.mapper")
public class MybatisConfiguration {

    @Bean
    public SqlSessionFactoryBean copSqlSessionFactoryBean(DataSource dataSource) {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);
        return sqlSessionFactoryBean;
    }

    /**
     * Admin core sql session template core sql session template.
     *
     * @param coreSqlSessionFactoryBean the admin core sql session factory bean
     * @return the core sql session template
     * @throws Exception the exception
     */
    @SuppressWarnings("PMD.SignatureDeclareThrowsException")
    @Bean
    public SqlSessionTemplate coreSqlSessionTemplate(SqlSessionFactoryBean coreSqlSessionFactoryBean) throws Exception {
        return new SqlSessionTemplate(coreSqlSessionFactoryBean.getObject());
    }
}
